<template>
  <div>
    <el-form ref="form" label-width="80px">
      <el-form-item label="spu名称"> {{spu.spuName}} </el-form-item>
      <el-form-item label="sku名称">
        <el-input placeholder="请输入sku名称" v-model="skuInfo.skuName"></el-input>
      </el-form-item>
      <el-form-item label="价格(元)">
        <el-input placeholder="价格(元)" type="number" v-model="skuInfo.price"></el-input>
      </el-form-item>
      <el-form-item label="重量(KG)">
        <el-input placeholder="重量(KG)" v-model="skuInfo.weight"></el-input>
      </el-form-item>
      <el-form-item label="规格描述">
        <el-input type="textarea" rows="4" v-model="skuInfo.skuDesc"></el-input>
      </el-form-item>
      <el-form-item label="品台属性">
        <!--行内表单:inline="true"-->
        <el-form ref="form" :inline="true" label-width="80px">
          <el-form-item :label="attr.attrName" v-for="attr in attrInfoList " :key="attr.id">
            <el-select placeholder="请选择" v-model="attr.attrIdAndValueId">
              <el-option :label="attrValue.valueName" v-for="attrValue in attr.attrValueList" :key="attrValue.id" :value="`${attr.id}:${attrValue.id}`"></el-option>
            </el-select>
          </el-form-item>
        </el-form>
      </el-form-item>
      <el-form-item label="销售属性">
        <!--行内表单:inline="true"-->
        <el-form ref="form" :inline="true" label-width="80px">
          <el-form-item :label="saleAttr.saleAttrName" v-for="saleAttr in spuSaleAttrList" :key="saleAttr.id">
            <el-select placeholder="请选择" v-model="saleAttr.attrIdAndValueId">
              <el-option :label="saleAttrValue.saleAttrValueName" v-for="saleAttrValue in saleAttr.spuSaleAttrValueList" :key="saleAttrValue.id" :value="`${saleAttr.id}:${saleAttrValue.id}`"></el-option>
            </el-select>
          </el-form-item>
        </el-form>
      </el-form-item>
      <el-form-item label="图片列表">
        <el-table border style="width: 100%" :data="spuImageList" @selection-change="handleSelectionChange">
          <el-table-column
            prop="prop"
            label=""
            width="80px"
            type="selection"
            align="center"
          >
          </el-table-column>
          <el-table-column prop="prop" label="图片" width="width">
            <template slot-scope="{row}">
              <img :src="row.imgUrl" alt="" style="width:100px;height:100px;">
            </template>
          </el-table-column>
          <el-table-column prop="imgName" label="名称" width="width">
          </el-table-column>
          <el-table-column prop="prop" label="操作" width="width">
            <template slot-scope="{row}">
              <el-button type="primary" size="small" v-if="row.isDefault==0" @click="changeDefault(row)">设为默认</el-button>
              <el-button  size="small" v-else >默认</el-button>
            </template>
          </el-table-column>
        </el-table>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="save">保存</el-button>
        <el-button @click="cancel">取消</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
export default {
  name: "SkuForm",
  data() {
    return {
      //存储图片
      spuImageList: [
        {id:1,imgName:'风景1',imgUrl:'/static/1.png'},
          {id:2,imgName:'风景2',imgUrl:'/static/2.png'},
            {id:3,imgName:'风景3',imgUrl:'/static/3.png'},
              {id:4,imgName:'风景4',imgUrl:'/static/4.png'},
                {id:5,imgName:'风景5',imgUrl:'/static/5.png'}
      ],
      //收集图片的字段
      imgList:[],
      //存储销售属性
      spuSaleAttrList: [],
      //存储属性品台的数据
      attrInfoList: [],
      //收集sku数据的字段
      skuInfo: {
        //第一类收集的数据:父组件给的数据
        category3Id: 0,
        spuId: 0,
        tmId: 0,
        //第二类需要数据双向绑定v-model收集
        skuName: "",
        price: 0,
        weight: "",
        skuDesc: "",
        //第三类需要自己书写代码
        //品台属性
        skuAttrValueList: [
          // {
          //   attrId: 0,
          //   valueId: 0, 
          // },
        ],
        skuDefaultImg: "",
       
        skuImageList: [
          // {
          //   id: 0,
          //   imgName: "string",
          //   imgUrl: "string",
          //   isDefault: "string",
          //   skuId: 0,
          //   spuImgId: 0,
          // },
        ],
        //收集销售属性列表
        skuSaleAttrValueList: [
          // {
          //   saleAttrId: 0,

          //   saleAttrValueId: 0,
          
          // },
        ],
       
      },
      spu:{

      },
    };
  },
  methods: {
    //保存
    async save(){
      //整理数据
      const {attrInfoList,skuInfo,spuSaleAttrList,imgList} = this
      //方式一
      // attrInfoList.forEach(item=>{
      //   //当前平台属性用户进行了选择
      //   if(item.attrIdAndValueId){
      //     const[attrId,valueId] = item.attrIdAndValueId.split(":")
      //     //携带给服务器不是数组而是对象
      //     let obj = {attrId,valueId}
      //     skuInfo.skuAttrValueList.push(obj)
      //   }
      // })
      //方式二
      skuInfo.skuAttrValueList = attrInfoList.reduce((prev,item)=>{
        if(item.attrIdAndValueId){
          const[attrId,valueId] = item.attrIdAndValueId.split(":")
          prev.push({attrId,valueId})
        }
        return prev
      },[])
      //整理销售
      skuInfo.skuSaleAttrValueList = spuSaleAttrList.reduce((prev,item)=>{
        if(item.attrIdAndValueId){
          const [saleAttrId,saleAttrValueId] = item.attrIdAndValueId.split(":")
          prev.push({saleAttrId,saleAttrValueId})
        }
        return prev
      },[])
      //整理图片的数据 map映射出一个新的数组
      skuInfo.skuImageList = imgList.map(item=>{
        return{
            imgName :item.imgName,
            imgUrl : item.imgUrl,
            isDefault : item.isDefault,
            spuImgId : item.id
        }
      })
      //发请求
      const result = await this.$API.sku.reAddSku(skuInfo);
      if(result.code==200){
        this.$message({type:'success',message:"添加sku成功"})
        this.$emit('changeScene',0)
      }
    },
    //取消
    cancel(){
      //自定义事件让父组件切换场景0
      this.$emit('changeScene',0)
      //清除数据
      Object.assign(this._data,this.$options.data())
    },
    changeDefault(row){
      this.spuImageList.forEach(item=>{
       item.isDefault = 0
      })
      row.isDefault = 1
      //收集默认图片的地址
      this.skuInfo.skuDefaultImg = row.imgUrl
    },
    //table表格复选框按钮的事件
    handleSelectionChange(params){
      this.imgList = params

    },
    //初始化数据
    async getData(category1Id, category2Id, spu) {
      this.spuImageList.forEach(item=>{
        item.spuId = spu.id
        this.$set(item,'isDefault',0)
      })
      //收集父组件的数据
      this.skuInfo.category3Id = spu.category3Id
      this.skuInfo.spuId = spu.id
      this.skuInfo.tmId = spu.tmId
      this.spu = spu
      const result = await this.$API.sku.getImagesList(spu.id);
      if (result == 200) {
        //this.spuImageList = result.data;
      }
      const result1 = await this.$API.sku.getSpuSaleAttrList(spu.id);
      if (result1.code == 200) {
        this.spuSaleAttrList = result1.data;
      }
      const result2 = await this.$API.sku.getAttrInfoList(
        category1Id,
        category2Id,
        spu.category3Id
      );
      if (result2.code == 200) {
        this.attrInfoList = result2.data;
      }
    },
  },
};
</script>

<style></style>
